package main

import (
	"fmt"
	"time"
)

//定时器
//单次触发：Timer(NewTimer或AfterFunc实现)   周期性触发：Ticker

func main() {
	// stop := false
	// time.AfterFunc(5*time.Second, func() {
	// 	stop = true
	// })
	// for {
	// 	if stop {
	// 		fmt.Println("exit...")
	// 		break
	// 	}
	// 	time.Sleep(1 * time.Second)
	// 	fmt.Println(time.Now())
	// }

	fmt.Println(time.Now())
	m := time.NewTimer(5 * time.Second)
	// m.Stop()
	fmt.Println(<-m.C)
	fmt.Println("exit now")

	tk := time.NewTicker(2 * time.Second)
	count := 1
	for {
		if count > 2 {
			tk.Stop()
			break
		}
		fmt.Println(<-tk.C)
		count++
	}

}
